package com.encryption.controls.confirm
{
	import com.encryption.controls.confirm.event.ConfirmDialogEvent;
	
	import flash.display.DisplayObject;
	
	import mx.core.FlexGlobals;
	import mx.managers.PopUpManager;

	/**
	 * 删除确认界面弹出管理类
	 * @author jill
	 * 
	 */
	public class ConfirmPopUpManager
	{
		private static var dialog:ConfirmTipDialog;
		/**
		 *  点确认后的执行函数
		 */
		private static var callBack:Function;
		
		/**
		 * 点取消后执行函数
		 */
		private static var cancelFunc:Function;
		public function ConfirmPopUpManager()
		{
		}
		
		public static function removePopUp(popUp:ConfirmTipDialog):void
		{
			PopUpManager.removePopUp(popUp);
		}
		
		/**
		 * 居中弹出删除确认对话框 
		 * @param confirmFun 点确认后的执行函数
		 * @param parent 对话框的父对话框
		 * @param message 删除对话框的提示信息
		 * 
		 */
		public static function centerPopUp(confirmFun:Function, parent:DisplayObject=null, message:String="你确定要删除吗?"):void
		{
			if(dialog == null)
				dialog = new ConfirmTipDialog();
			
			if(dialog.isPopUp)
				PopUpManager.removePopUp(dialog);
			
			callBack = confirmFun;
			cancelFunc = null;
			if(callBack != null && !dialog.hasEventListener(ConfirmDialogEvent.CONFIRM)){
				dialog.addEventListener(ConfirmDialogEvent.CONFIRM, function(event:ConfirmDialogEvent):void{
					if(callBack != null)
						callBack.call();
				});
			}
			
			dialog.message = message;
			if(parent == null)
				parent = FlexGlobals.topLevelApplication as DisplayObject;
			PopUpManager.addPopUp(dialog, parent, true);
			PopUpManager.centerPopUp(dialog);
		}
		
		/**
		 * 居中弹出删除确认对话框 
		 * @param confirmFun 点确认后的执行函数
		 * @param cancelFun 点取消后的执行函数
		 * @param parent 对话框的父对话框
		 * @param message 删除对话框的提示信息
		 */
		public static function centerPopUpWithCancel(confirmFun:Function,cancelFun:Function, parent:DisplayObject=null, message:String="你确定要删除吗?"):void{
			if(dialog == null)
				dialog = new ConfirmTipDialog();
			
			if(dialog.isPopUp)
				PopUpManager.removePopUp(dialog);
			
			callBack = confirmFun;
			cancelFunc = cancelFun;
			if(callBack != null && !dialog.hasEventListener(ConfirmDialogEvent.CONFIRM)){
				dialog.addEventListener(ConfirmDialogEvent.CONFIRM, function(event:ConfirmDialogEvent):void{
					if(callBack != null)
						callBack.call();
				});
			}
			
			if(cancelFunc != null && !dialog.hasEventListener(ConfirmDialogEvent.CANCLE)){
				dialog.addEventListener(ConfirmDialogEvent.CANCLE, function(event:ConfirmDialogEvent):void{
					if(cancelFunc != null)
						cancelFunc.call();
				});
			} 
			
			dialog.message = message;
			if(parent == null)
				parent = FlexGlobals.topLevelApplication as DisplayObject;
			PopUpManager.addPopUp(dialog, parent);
			PopUpManager.centerPopUp(dialog);
		}
	}
}